<template>
  <div class="newLabelContainer">
    <div
      class="dialog animated"
      :class="open ? 'fadeIn' : 'fadeOut'"
      v-show="show"
      :style="{ visibility: visible ? 'visible' : 'hidden' }"
    >
      <div class="panel animated" :class="open ? 'zoomIn' : ''">
        <div class="title">
          新增标签
          <div class="close" @click="cancel()"></div>
        </div>
        <el-form label-width="140px">
          <el-form-item label="标签名称" :rules="[{ required: true, message: '' }]">
            <el-input
              clearable
              style="width:70%"
              ref="tagName"
              v-model="tagObject.tagName"
              placeholder="请输入标签名称"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="btn_div">
          <div class="btn_sure" @click="sure()">确 认</div>
          <div class="btn_cancel" @click="cancel()">取 消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var vm,user;
export default {
  data: function() {
    return {
      show: false,
      open: false,
      visible: false,
      isAdd: true,
      tagObject: {
        orgId: "",
        tagName: ""
      }
    };
  },
  methods: {
    init: function(data) {
      vm.show = true;
      setTimeout(function() {
        vm.open = true;
        vm.visible = true;
      }, 11);
      if (data.tagId == undefined) {
        vm.isAdd = true;
        vm.tagObject = {
          orgId: user.orgId,
          tagName: ""
        };
      } else {
        vm.isAdd = false;
        vm.tagObject = data;
      }
    },
    sure: function() {
      var rules = [];
      let tagName = this.$refs.tagName;
      rules.push({
        judge: tool.judge("classname", vm.tagObject.tagName),
        focus: tagName,
        tips: "请输入只含汉字字母_-()且20个字符以内标签名称"
      });
      if (!tool.validate(rules)) {
        return;
      }
      vm.tagObject._wait = "";
      if (vm.isAdd) {
        api.tagAdd(vm.tagObject).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "添加成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      } else {
        api.tagUpdate(vm.tagObject).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "编辑成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      }
    },
    cancel: function() {
      //隐藏弹窗
      vm.open = false;
      vm.isAdd = true;
      setTimeout(function() {
        vm.show = false;
        vm.visible = false;
      }, 500);
    }
  },
  created: function() {
    vm = this;
     user = tool.getItem(lsKey.user, true); //得到当前用户信息
  }
};
</script>
<style lang="scss">
.newLabelContainer {
  .el-form-item__label {
    font-size: 0.16rem;
    color: #666666;
  }
  .el-checkbox__label {
    font-size: 0.16rem;
    color: #666666;
  }
}
</style>
<style scoped src="@/style/module/dialog.css"></style>
<style scoped>
.panel {
  width: 6rem;
}
</style>





